/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
    cate.init();
});

var cate = (function() {

    function addNewCate() {
        var cateName = $('#cateName').val(),
                cateCode = $('#cateCode').val(),
                idcate_Pa = $('#postCateParent').val();

        $('#cateInput .form-group').removeClass('has-error');
        $('#cateAlert').hide().html('');
        if (!cateName) {
            $('#cateName').parent('.form-group').addClass('has-error');
            return false;
        }
        if (!cateCode) {
            $('#cateCode').parent('.form-group').addClass('has-error');
            return false;
        }

        $.post('/newscate/ajax-add-new-newscate', {
            name: cateName,
            code: cateCode,
            idcate_Pa: idcate_Pa
        }, function(resp) {
            if (resp.err == 0) {
                //var data = resp.data;
//                ,
//                    html = '<tr>\
//                                <td class="name">\
//                                    <a data-title="Tên danh mục" data-pk="' + data.id + '" data-type="text" href="#" class="editable editable-click">\
//                                    ' + cateName + '\
//                                    </a>\
//                                </td>\
//                                <td class="text-center">\
//                                    <a href="#" class="text-danger" onclick="return cate.deleteCate(this, \'' + data.id + '\')"><i class="fa fa-trash-o"></i></a>\
//                                </td>\
//                            </tr>';
//                $('#listCate').find('tbody').prepend(html);
//                $('#cateName').val('');
                  window.location.href = '/newscate/edit?id=' + resp.data.id + '#msg=' + resp.msg;
            } else {
                $('#cateAlert').show().html(resp.msg);
            }
            $('#addCate').find('i.fa').attr('class', 'fa fa-plus-circle');
        }, 'json');
    }


    function editCate(){   
        var cateID = $('#cateID').val(),
                cateName = $('#cateName').val(),
                cateCode = $('#cateCode').val(),
                idcate_Pa = $('#postCateParent').val();
                
        $('.form-group').removeClass('has-error');
        $('.errorHandler').html('');        
        if (!cateName) {
            $('#cateName').parent('.form-group').addClass('has-error');
            return false;
        }
        if (!cateCode) {
            $('#cateCode').parent('.form-group').addClass('has-error');
            return false;
        }
                 
        $.post('/newscate/ajax-edit-newscate', {
                        idCate: cateID,
                        name: cateName,
                        code: cateCode,
                        idcate_Pa: idcate_Pa
                    }, function(resp) {
                        if(resp.err == 0) {
                            $('#editCate').find('i').attr('class','fa fa-save');
                            $('.errorHandler').html('<small><small><strong class="text-success">' + resp.msg + '</strong></small></small>').show();   
                        } else {
                            $('.errorHandler').html('<small><small><strong class="text-danger">' + resp.msg + '</strong></small></small>').show();
                        }
                    }, 'json');        
    }


    function deleteCateBn(id, cateName, callback){
        showConfirm({
            msg: 'Bạn có chắc chắn muốn xoá danh mục <strong class="text-danger">' + cateName +'</strong> không?',
            title: 'Xóa danh mục',
            noBt: 'Đóng',
            yesBt: 'Đồng ý xoá',
            onYes: function() { 
                $.post('/newscate/ajax-delete-newscate', {id: id}, function(resp) {
                    if(resp.err === 0){
                        callback();
                        hideConfirm();
                    }else {
                         hideConfirm();
                        $('.errorHandler').removeClass('text-success').addClass('text-danger').html(resp.msg).show();
                    }
                }, 'json');
            }
        });
    }





    function initHandler() {
        $('#addCate').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            addNewCate();
            return false;
        });

        $('#editCate').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            editCate();
            return false;
        });

        $('#deleteCateBn').click(function() {
            var id = $('#cateID').val(),
                cateName = $('#cateName').val();
            deleteCateBn(id, cateName, function() {
                window.location.href='/newscate';
            });
            return false;
        });

        var hash = window.location.hash;
        if(hash.indexOf('#msg=') > -1){
            $('.errorHandler').html(hash.substring(5)).show();
            window.location.hash = '';
        }

//        $('#listCate').editable({
//            selector: 'a.cate_name',
//            mode: 'inline',
//            url: '/newscate/ajax-edit-newscate',
//            pk: 1,
//                ajaxOptions: {
//                type: 'post',
//                dataType: 'json'
//            }
//        });
    }

    return {
        init: function() {
            initHandler();
        },
        
        
        deleteCateBn: function(cateId, cateName, obj){
            deleteCateBn(cateId, cateName, function(){
                $(obj).remove();
            });
            return false;
        },
        
        
        deleteCate: function(obj, id) {
            var tr = $(obj).parents('tr'),
                name = tr.find('td.name').find('a').html();
            showConfirm({
                msg: 'Bạn có chắc chắn muốn xoá danh mục <span class="text-danger">"' + name + '"</span> không?',
                title: 'Xoá danh mục',
                noBt: 'Đóng',
                yesBt: 'Đồng ý xoá',
                onYes: function(iid) { 
                    $.post('/newscate/ajax-delete-newscate', {id: iid}, function(resp) {
                        if(resp.err == 0) {
                            tr.remove();
                            hideConfirm();
                        }
                    }, 'json');
                },
                param: id
            });
            return false;
        }
    };
})();